package com.zwj.controller;

import com.zwj.service.FileService;
import com.zwj.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/file")
public class FileController {
    private final FileService fileService;
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }
//    @ResponseBody
//    //单附件上传
//    @ApiOperation(value = "上传附件", notes = "上传附件接口")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name = "uId",value = "上传用户ID，",paramType = "query",required = true,dataType = "int")
////    })
//    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
//    public Result upload(@RequestPart MultipartFile file) {
//        return fileService.upload(file);
//    }
//
////    //多附件上传
////    @ApiOperation(value = "上传多个附件 RequestParam", notes = "上传多个附件接口 RequestParam")
////    @PostMapping(value = "/a_t_s/uploads", headers = "content-type=multipart/form-data")
////    public Result uploads(@RequestPart MultipartFile[] files) {
////        return commonService.uploads(files);
////    }
//
//    //下载附件 根据附件存储名
//    @ApiOperation(value = "下载附件", notes = "下载附件接口")
//    @PostMapping(value = "/download")
//    public void download(@RequestBody FileModuleBo fileModuleBo, ServletResponse servletResponse) {
//        fileService.download(fileModuleBo.getFileName(), (HttpServletResponse) servletResponse);
//    }

    @ResponseBody
    //单附件上传
    @ApiOperation(value = "上传附件 新", notes = "上传附件接口 新")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uId",value = "上传用户ID，",paramType = "query",required = true,dataType = "int")
//    })
    @PostMapping(value = "/uploadFile", headers = "content-type=multipart/form-data")
    public Result uploadFile(@RequestPart MultipartFile file) {
        return fileService.uploadFile(file);
    }
    @ApiOperation(value = "下载附件 新", notes = "下载附件 新")
    @GetMapping("/download/{fileName:.+}")
    @ResponseBody
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) throws UnsupportedEncodingException {
        return fileService.downloadFile(fileName, request);
    }
    @ResponseBody
    //单附件上传
    @ApiOperation(value = "上传头像 新", notes = "上传头像 新")
    @PostMapping(value = "/uploadImage", headers = "content-type=multipart/form-data")
    public Result uploadImage(@RequestPart MultipartFile file) {
        return fileService.uploadImage(file);
    }
}
